package cn.darksoul3.common.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Collection;
import java.util.Collections;

/**
 * <p>API集合类返回结果</p>
 *
 * @author fuzongyao
 * @date 2019-07-02 12:32
 * @since 1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DataResult<T> extends BaseResult {
    private Collection<T> data;

    public DataResult(CodeMsg codeMessage) {
        super(codeMessage);
    }

    public static <T> DataResult<T> succeed(Collection<T> data) {
        DataResult<T> result = new DataResult<>(GlobalCodeMsgEnum.SUCCESS);
        result.data = data;
        return result;
    }

    public static <T> DataResult<T> succeedWithEmptyData() {
        DataResult<T> result = new DataResult<>(GlobalCodeMsgEnum.SUCCESS);
        result.data = Collections.emptyList();
        return result;
    }

    public static <T> DataResult<T> fail(CodeMsg codeMessage) {
        return new DataResult<>(codeMessage);
    }

    public Collection<T> getData() {
        return data;
    }

    public DataResult<T> setData(Collection<T> data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return "DataResult{" +
                "code=" + this.getCode() +
                ", message='" + this.getMessage() + '\'' +
                ", data=" + data +
                "} ";
    }
}
